<?php
if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );  
class vm_ps_order_list{
    function getOrderList() {
        global $VM_LANG, $CURRENCY_DISPLAY, $sess, $limit, $limitstart, $mm_action_url;
        
        $ps_vendor_id = $_SESSION["ps_vendor_id"];
        $auth = $_SESSION['auth'];
        require_once( CLASSPATH .'ps_order_status.php');
        require_once( CLASSPATH .'htmlTools.class.php');
        require_once( CLASSPATH .'pageNavigation.class.php');
        
        $db = new ps_DB;
        $dbl = new ps_DB;
        
        $list  = "SELECT o.order_id, o.order_status, o.cdate, o.mdate, o.order_total, o.order_currency, o.user_id,
                    o_u_i.first_name, o_u_i.last_name, o_s.order_status_name 
                 FROM #__{vm}_orders o
                    LEFT JOIN #__{vm}_order_user_info o_u_i ON (o.order_id = o_u_i.order_id AND o_u_i.address_type = 'BT')
                    LEFT JOIN #__{vm}_order_status o_s ON (o.order_status = o_s.order_status_code)
                 WHERE 1=1";
                 
        $count = "SELECT count(o.order_id) as num_rows FROM #__{vm}_orders o 
                    LEFT JOIN #__{vm}_order_user_info o_u_i ON (o.order_id = o_u_i.order_id AND o_u_i.address_type = 'BT')
                    LEFT JOIN #__{vm}_order_status o_s ON (o.order_status = o_s.order_status_code)
                  WHERE 1=1";
        
        // Keyword
        $keyword = JRequest::getVar('keyword');
        
        $q = "";
        if (!empty($keyword)) {
            $q  .= " AND (o.order_id LIKE '%$keyword%' ";
            $q .= "OR o.order_status LIKE '%$keyword%' ";
            $q .= "OR o_u_i.first_name LIKE '%$keyword%' ";
            $q .= "OR o_u_i.last_name LIKE '%$keyword%' ";
            $q .= "OR CONCAT(o_u_i.first_name, ' ', o_u_i.last_name) LIKE '%$keyword%' ";
            $q .= ") ";
        }
        
        // Start Date
        $start_date=JRequest::getVar('start_date');
        $start_date = str_replace('-', '/', $start_date);
        if (empty($start_date)) {
            $start_date = mktime(0,0,0,date("m"),date("d")-3,date("Y"));
        } else {
            $start_date = strtotime($start_date);    
        }
        
        // End Date
        $end_date=JRequest::getVar('end_date');
        $end_date = str_replace('-', '/', $end_date);
        if (empty($end_date)) {
            $end_date = mktime(0,0,0,date("m"),date("d")-3,date("Y"));
        } else {
            $end_date = strtotime($end_date);    
        }
        $q .= " AND o.cdate between ".$start_date." and ".$end_date." "; 
        
        // Order Status
        $order_status = JRequest::getVar('cbo_order_status');
        if ($order_status != '') {
            $q .= " AND o.order_status = '".$order_status."'"; 
        } 
        
        // Vendor_id
        $vendor_id = (int)JRequest::getVar('hdn_vendor_id');
        if ($vendor_id > 0) {
            $q .= " AND o.vendor_id=".$vendor_id;
        }
        
        $q .= " ORDER BY o.cdate DESC ";
        $list .= $q . " LIMIT $limitstart, " . $limit;
        $count .= $q; 

        $db->query($count);
        $db->next_record();
        $num_rows = $db->f('num_rows');  
        
        $pageNav = new vmPageNav( $num_rows, $limitstart, $limit );
        
        $listObj = new listFactory( $pageNav );
        $listObj->writeSearchHeaderByOrderList( '', '', 'account', 'order_list');
        
        $db->query( $list );
        if( $num_rows == 0 ) {
            echo " (<span style=\"font-style:italic;font-weight: bold;\">".$VM_LANG->_('PHPSHOP_ACC_NO_ORDERS')."</span>)\n";
            return;
        }
        
        // start the list table
        $attributes = "cellpadding=0 cellspacing=0";
        $listObj->startTable($attributes);
        
        $columns = Array(  "#" => "style='width: 50px;color: #000000; border-bottom: solid 1px #cccccc;'", // 1
                        'Order Number' => "style='color: #000000; border-bottom: solid 1px #cccccc;'", // 2                        
                        'Order Date' => "style='color: #000000; border-bottom: solid 1px #cccccc;'", // 3
                        'Last Modified' => "style='color: #000000; border-bottom: solid 1px #cccccc;'", // 4
                        'Status' => "style='color: #000000; order-bottom: solid 1px #cccccc;'", // 5
                        'Total' => "style='color: #000000; border-bottom: solid 1px #cccccc;'", // 6
                        'Restaurant' => "style='color: #000000; border-bottom: solid 1px #cccccc;'", // 7
                        'Menutise (%)' => "style='color: #000000; border-bottom: solid 1px #cccccc;'" // 8
                );
        $listObj->writeTableHeader( $columns );
        
        $listObj->alternateColors = array( 0 => '', 1 => '' );
        
        $i = 0;
        $total_total = 0;
        $total_restaurant = 0;
        $total_menutise = 0;

        while ($db->next_record()) {
            $listObj->newRow('', '', 'style = "color: #000000; background-color: #FFFFFF; "');
            
            // 1. The row number
            $listObj->addCell( $pageNav->rowNumber( $i ), 'style="border-bottom: solid 1px #cccccc;"' );
            
            // 2. Order Number
            $tmp_cell = sprintf("%08d", $db->f("order_id"));
            $listObj->addCell( $tmp_cell, 'style="border-bottom: solid 1px #cccccc;"' );
                        
            // 3. Order Date
            $listObj->addCell( vmFormatDate($db->f("cdate"), "%d-%b-%y %H:%M"), 'style="border-bottom: solid 1px #cccccc;"');
            
            // 4. Last Modified
            $listObj->addCell( vmFormatDate($db->f("mdate"), "%d-%b-%y %H:%M"), 'style="border-bottom: solid 1px #cccccc;"');
            
            // 5. Status
            $listObj->addCell( $db->f("order_status_name"), 'style="border-bottom: solid 1px #cccccc;"');
            
            // 6. Total
            $total = $db->f("order_total");
            $total_total += $total;
            $listObj->addCell( $GLOBALS['CURRENCY_DISPLAY']->getFullValue($total, '', $db->f('order_currency')), 'style="border-bottom: solid 1px #cccccc;"' );
            
            // 7. Restaurant
            $menutise = (double)USE_AS_PERCENT*$total / 100;
            $total_menutise += $menutise;
            $restaurant = $total - $menutise;
            $total_restaurant += $restaurant;
            $listObj->addCell( $GLOBALS['CURRENCY_DISPLAY']->getFullValue($restaurant, '', $db->f('order_currency')), 'style="border-bottom: solid 1px #cccccc;"' );
    
            // 8. Menutise (%)
            $listObj->addCell( $GLOBALS['CURRENCY_DISPLAY']->getFullValue($menutise, '', $db->f('order_currency')), 'style="border-bottom: solid 1px #cccccc;"' );
                                                
            $i++;
        }
        
        // Group Footer - Start
        $listObj->newRow();
        $listObj->addCell("Total:", "style='font-weight: bold;'");
        $listObj->addCell($i, "style='font-weight: bold;'");
        $listObj->addCell("");
        $listObj->addCell("");
        $listObj->addCell("");
        
        // Total
        $listObj->addCell( $GLOBALS['CURRENCY_DISPLAY']->getFullValue($total_total, '', $db->f('order_currency')), "style='font-weight: bold;'" );

        // Restaurant
        $listObj->addCell( $GLOBALS['CURRENCY_DISPLAY']->getFullValue($total_restaurant, '', $db->f('order_currency')), "style='font-weight: bold;'" );

        // Menutise
        $listObj->addCell( $GLOBALS['CURRENCY_DISPLAY']->getFullValue($total_menutise, '', $db->f('order_currency')), "style='font-weight: bold;'" );
        // Group Footer - End
        
        $listObj->writeTable();
        $listObj->endTable();
    }
}

// Check if there is an extended class in the Themes and if it is allowed to use them
// If the class is called outside Virtuemart, we have to make sure to load the settings
// Thomas Kahl - Feb. 2009
if (!defined('VM_ALLOW_EXTENDED_CLASSES') && file_exists(dirname(__FILE__).'/../virtuemart.cfg.php')) {
    include_once(dirname(__FILE__).'/../virtuemart.cfg.php');
}
// If settings are loaded, extended Classes are allowed and the class exisits...
if (defined('VM_ALLOW_EXTENDED_CLASSES') && defined('VM_THEMEPATH') && VM_ALLOW_EXTENDED_CLASSES && file_exists(VM_THEMEPATH.'user_class/'.basename(__FILE__))) {
    // Load the theme-user_class as extended
    include_once(VM_THEMEPATH.'user_class/'.basename(__FILE__));
} else {
    // Otherwise we have to use the original classname to extend the core-class
    class ps_order_list extends vm_ps_order_list {}
}


$ps_order_list = new ps_order_list;
?>
